package utils.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import com.example.cd5160866.materialdesgin.R;
import com.example.cd5160866.materialdesgin.SecondActivity;

import view.InitService;

/**
 * Created by cd5160866 on 2017/10/27.
 */

public class NotificationUtil {

    public static void simple(Context context) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        //必须属性
        builder.setSmallIcon(R.mipmap.icon_more)
                .setColor(Color.parseColor("#1287FF"));

        //内容描述
        builder.setContentTitle("标题");
        builder.setContentText("这是通知内容");
        builder.setTicker("你收到一条通知");

        //进度样式
        //                builder.setProgress(100,30,false);
        //                builder.setProgress(0, 0, false)若如此设置 在有进度条的情况下 一般移除进度条

        builder.setOngoing(false);// 一般和progress 配合使用 让通知无法被直接抹掉 如果要流氓点也行
        //        builder.setAutoCancel(false) 与setOngoing 效果一样

        //                builder.setDefaults(Notification.DEFAULT_ALL);//带声音 震动  灯光 跟 手机rom有关 推荐都添加这个属性 因为各个厂商的效果不一样 特别是灯光 譬如魅族 就只有白灯
        //                builder.setLights(Color.BLUE,200,200);
        //        builder.setVibrate(new long[]{300,300,200});
        //                builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        //                builder.setSound(Uri.parse("file:///sdcard/test.mp3"));

        //可以用于启动本地Activity ，启动Service ，发送BroadCast 也可隐式意图打开浏览器 拨号等。。。
        //        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, getOpenUrlIntent(), PendingIntent.FLAG_CANCEL_CURRENT);
        //        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true); //通常和pendingIntent 配合使用 点击即消除掉通知


        //4.2 above
        //        builder.setPriority(Notification.PRIORITY_MAX); 设置优先级 越高在通知栏越置顶 若同时都是最高优先级 则最新来的通知替换老的位置
        //        5.0 above 类似于ios 的banner通知 部分机型生效
        //        builder.setFullScreenIntent(pendingIntent, true);


        Notification notification = builder.build();
        //        notification.vibrate = new long[]{300,300,200}; 也可直接对notification对象进行操作
        //        notification.flags = Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONGOING_EVENT; flags 也可以对notification的 属性进行操作

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1, notification);
    }

    public static void simpleActivity(Context context) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, new Intent(context, SecondActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_small)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_women))
                .setContentTitle("露露")
                .setContentText("其实你是一个好人，我们还是做朋友吧")
                .setTicker("露露发来一条消息")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                //                .setFullScreenIntent(pendingIntent, false)
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(2, notification);
    }

    public static Notification simpleService(Context context) {
        PendingIntent pendingIntent = PendingIntent.getService(context, 1, new Intent(context, InitService.class), PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_small)
                .setContentTitle("陌陌")
                .setContentText("这是一个很牛的社交软件")
                .setContentIntent(pendingIntent)
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(10, notification);
        return notification;
    }


    public static void progress(Context context, int progress, boolean isIndeterminate) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setSmallIcon(R.mipmap.ic_small)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_women));
        builder.setTicker("当前进度");
        builder.setContentTitle("进行中");
        builder.setOngoing(true);
        builder.setProgress(100, progress, isIndeterminate);
        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(3, notification);
    }

    public static void closeProgress(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(3);
    }


    public static Intent getOpenUrlIntent() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://www.baidu.com");
        intent.setData(content_url);
        return intent;
    }


}
